﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Peekaboo2008.dao;
using Peekaboo2008.model;

namespace Peekaboo2008.view
{
    public partial class FormChiTietNhomHangHoa : Form
    {
        private FormNhomHangHoa formNhomHang;
        private NhomHangModel nhomHangModel;
        private FormChiTietSanPhamHangHoa formChiTietSanPhamHangHoa;
        private int flag = 0; 

        public FormChiTietNhomHangHoa(ref FormNhomHangHoa _formNhomHang)
        {
            InitializeComponent();
            FormChiTietNhomHangLoad();
            formNhomHang = _formNhomHang;
        }
        public FormChiTietNhomHangHoa(ref FormChiTietSanPhamHangHoa _formChiTietSanPhamHangHoa)
        {
            InitializeComponent();
            FormChiTietNhomHangLoad();
            formChiTietSanPhamHangHoa = _formChiTietSanPhamHangHoa;
            flag = 1;
        }

        public FormChiTietNhomHangHoa(NhomHangModel _nhomHangModel, ref FormNhomHangHoa _formNhomHang)
        {
            InitializeComponent();
            FormChiTietNhomHangLoad();
            formNhomHang = _formNhomHang;
            nhomHangModel = _nhomHangModel;
            txtTenNhomHang.Text = nhomHangModel.Ten;
            txtVAT.Text = nhomHangModel.Thue;
            cbbNganhHang.SelectedValue = nhomHangModel.MaNganhHang;
            rtbGhiChu.Text = nhomHangModel.GhiChu;
            txtChietKhau.Text = nhomHangModel.ChietKhau;
        }

        public void FormChiTietNhomHangLoad()
        {
            DataTable dtNganhHang = NganhHangDAO.getAllOrderByName();
            cbbNganhHang.DataSource = dtNganhHang;
            cbbNganhHang.ValueMember = "ID";
            cbbNganhHang.DisplayMember = "Ten";
        }

        private void FormChiTietNhomHang_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormChiTietNhomHang_KeyDown);
        }

        private void FormChiTietNhomHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8)
            {
                btnChapNhan.PerformClick();
            }
            if (e.KeyCode == Keys.F6)
            {
                btThemNganhHang.PerformClick();
            }
            if (e.KeyCode == Keys.F12)
            {
                this.Dispose();
            }
        }


        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            if (txtTenNhomHang.Text.Equals(""))
            {
                MessageBox.Show("Tên Nhóm Hàng Không Được Trống");
                txtTenNhomHang.Focus();

            }
            else
            {
                if (nhomHangModel == null)
                {
                    nhomHangModel = new NhomHangModel();
                }
                nhomHangModel.GhiChu = rtbGhiChu.Text;
                nhomHangModel.MaNganhHang = cbbNganhHang.SelectedValue.ToString();
                nhomHangModel.Ten = txtTenNhomHang.Text;
                nhomHangModel.Thue = txtVAT.Text;
                nhomHangModel.ChietKhau = txtChietKhau.Text;
                //new
                if (nhomHangModel.Id == null || nhomHangModel.Id == "")
                {
                    if (NhomHangDAO.insert(nhomHangModel))
                    {
                        MessageBox.Show("Thêm nhóm hàng hoá thành công");
                        if (flag == 1)
                        {
                            formChiTietSanPhamHangHoa.FormChiTietSanPhamHangHoaLoad();
                        }
                        else
                        {
                            formNhomHang.NhomHangHoaLoad();
                        }
                        this.Dispose();
                    }
                }
                //update
                else
                {
                    if (NhomHangDAO.update(nhomHangModel))
                    {
                        MessageBox.Show("Cập nhật nhóm hàng hoá thành công");
                        formNhomHang.NhomHangHoaLoad();
                        this.Dispose();
                    }
                }
            }
        }

        private void btThemNganhHang_Click(object sender, EventArgs e)
        {
            FormChiTietNhomHangHoa formChiTietNhomHangHoa = this;
            FormChiTietNganhHang formNganhHang = new FormChiTietNganhHang(ref formChiTietNhomHangHoa);
            formNganhHang.ShowDialog();
        }

        private void txtVAT_TextChanged(object sender, EventArgs e)
        {
            if (txtVAT.Text.Length > 2)
            {
                string VAT = txtVAT.Text;
                txtVAT.Text = VAT.Substring(1, 2);
                txtVAT.SelectionStart = 2;
            }
            txtVAT.KeyPress += new KeyPressEventHandler(txtVAT_KeyPress);

        }

        private void txtVAT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < '0') || (e.KeyChar > '9')) e.Handled = true;
        }

        private void txtChietKhau_TextChanged(object sender, EventArgs e)
        {
            if (txtChietKhau.Text.Length > 2)
            {
                string ChietKhau = txtChietKhau.Text;
                txtChietKhau.Text = ChietKhau.Substring(1, 2);
                txtChietKhau.SelectionStart = 2;
            }
            txtChietKhau.KeyPress += new KeyPressEventHandler(txtChietKhau_KeyPress);
        }

        private void txtChietKhau_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < '0') || (e.KeyChar > '9')) e.Handled = true;
        }

        private void btnKetThuc_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

       
    }
}
